package com.stephentuso.welcomeexample.pageview;

import com.stephentuso.welcome.WelcomePage;
import com.stephentuso.welcome.WelcomeUtils;
import com.stephentuso.welcome.util.PageView;
import com.stephentuso.welcomeexample.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * A simple {@link Fraction} subclass.
 */
public class ExamplePageView extends PageView implements WelcomePage.OnChangeListener {

    private ComponentContainer rootLayout;

    public ExamplePageView(Context context) {
        super(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_page_view_example, this, true);
        initComponent(component);
    }

    private void initComponent(Component component) {

        rootLayout = (ComponentContainer) component.findComponentById(ResourceTable.Id_layout);

        component.findComponentById(ResourceTable.Id_button).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                ToastDialog toastDialog = new ToastDialog(getContext());
                toastDialog.setText("Button clicked in custom PageView");
                ((Text)toastDialog.getComponent()).setMultipleLine(true);
                toastDialog.setAlignment(TextAlignment.BOTTOM);
                toastDialog.show();
            }
        });
    }

    @Override
    public void onWelcomeScreenPageScrolled(int pageIndex, float offset, int offsetPixels) {
        /*if (rootLayout != null)
            WelcomeUtils.applyParallaxEffect(rootLayout, true, offsetPixels, 0.3f, 0.2f);*/
    }

    @Override
    public void onWelcomeScreenPageSelected(int pageIndex, int selectedPageIndex) {
        //Not used
    }

    @Override
    public void onWelcomeScreenPageScrollStateChanged(int pageIndex, int state) {
        //Not used
    }
}
